package com.kaola.service.impl;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.kaola.dao.ProductDAO;
import com.kaola.pojo.Category;
import com.kaola.pojo.Product;
import com.kaola.service.ProductService;

public class ProductServiceImpl implements ProductService {

	public static final String REGEX = "^(0|[1-9][0-9]{0,9})(\\.[0-9]{1,2})?$";
	
	private ProductDAO productDAO;
	
	public ProductDAO getProductDAO() {
		return productDAO;
	}

	public void setProductDAO(ProductDAO productDAO) {
		this.productDAO = productDAO;
	}

	public void add(Product product) {
		productDAO.add(product);
	}

	public List<Product> list(Category category) {
		return productDAO.list(category);
	}

	public Product get(int id) {
		return productDAO.get(id);
	}

	public void modify(Product product) {
		productDAO.modify(product);
	}

	public void delete(int id) {
		productDAO.delete(id);
	}

	public boolean verifyPrice(double price) {
		Pattern pattern = Pattern.compile(REGEX);
		Matcher matcher = pattern.matcher(Double.toString(price));
		boolean isMatch = matcher.matches();
		return isMatch;
	}

	public int correctionInventory(int inventory) {
		if (inventory < 0) {
			inventory = 0;
		}
		return inventory;
	}

}
